<template>
  <div class="channle-edit">
    <van-cell :border="false">
      <div slot="title" class="title">我的频道</div>
      <van-button
        type="default"
        size="small"
        plain
        class="edit"
        round
        slot="default"
        @click="isShowIcon = !isShowIcon"
        >{{ isShowIcon ? "完成" : "编辑" }}</van-button
      >
    </van-cell>
    <!-- 频道内容 -->
    <van-grid :border="false">
      <van-grid-item
        class="my-grid zidingyi"
        v-for="(item, index) in channleEdit"
        :key="item.id"
      >
        <van-icon
          name="clear"
          slot="icon"
          @click="removeArticle(item.id, index)"
          v-show="isShowIcon && item.id !== 0 && active !== index"
        />
        <span
          slot="text"
          @click="updataActive(index)"
          :class="active === index ? 'active' : ''"
          >{{ item.name }}</span
        >
      </van-grid-item>
    </van-grid>
    <!-- /频道内容 -->

    <van-cell :border="false">
      <div slot="title" class="title">频道推荐</div>
    </van-cell>
    <!-- 频道内容 -->
    <van-grid :border="false" direction="horizontal">
      <van-grid-item
        class="my-grid grid-edit"
        v-for="value in recommit"
        icon="plus"
        :key="value.id"
        :text="value.name"
        @click="addAritcle(value)"
      />
    </van-grid>
    <!-- /频道内容 -->
  </div>
</template>

<script>
import { ArticleAll } from "@/api/article.js";
export default {
  created() {
    this.getList();
  },
  data() {
    return {
      articleAll: [],
      isShowIcon: false,
    };
  },
  props: {
    channleEdit: {
      type: Array,
      require: true,
    },
    active: {
      type: Number,
      require: true,
    },
  },
  methods: {
    // 请求全部频道
    async getList() {
      try {
        const { data: res } = await ArticleAll();
        this.articleAll = res.data.channels;
      } catch (err) {
        console.log(err);
      }
    },
    // 添加到我的频道
    addAritcle(val) {
      this.$emit("addArticle", val);
    },
    // 修改频道 active
    updataActive(index) {
      this.$emit("updataActive", index);
    },
    //删除我的频道
    removeArticle(id, index) {
      this.$emit("removeArticle", id, index);
    },
  },
  computed: {
    recommit() {
      return this.articleAll.filter(
        (item) => !this.channleEdit.find((item2) => item2.id === item.id)
      );
    },
  },
};
</script>

<style lang="less" scoped>
.channle-edit {
  padding-top: 85px;

  .title {
    color: #333333;
    font-size: 32px;
  }
  .edit {
    color: #aaa;
    font-size: 30px;
    padding: 2px 25px;
    border: 1px solid #aaa;
  }
  /deep/ .van-grid {
    padding: 0 22px;

    .zidingyi {
      .van-grid-item__content {
        i {
          position: absolute;
          top: 0;
          right: 0;
        }
      }
    }

    .my-grid {
      width: 160px;
      height: 85px;
      padding: 10px;
      .van-grid-item__content {
        border-radius: 10px;
        background-color: #f4f5f6;
        span {
          font-size: 28px;
          white-space: nowrap;
          color: #222222;
          margin: 0;
          margin-left: 4px;
        }
        i {
          font-size: 30px;
        }
        .active {
          color: red;
        }
      }
    }
  }
  /deep/ .van-grid-item__icon-wrapper {
    position: static;
  }
}
</style>